#!/usr/bin/env python
# -*- coding: utf-8 -*-

#to ensure the utf8 encoding environment
import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)
import base64
import re
import htmlentitydefs
import time
import urllib,urllib2,Cookie
from configuration import Configuration
from google.appengine.api import xmpp
from google.appengine.api import urlfetch
from google.appengine.ext import db

try:
	c=db.GqlQuery("SELECT * FROM Configuration")
	weibo_bot = c[0].weibo_bot
	twitter_id = c[0].twitter_id
except:
	#App may not have been initialized.
	exit(0)

class Twitter(db.Model):
    id=db.StringProperty()
    created = db.DateTimeProperty(auto_now_add=True)

#get one page of to user's replies, 20 messages at most. 
def parseTwitter(twitter_id,since_id="",):
	if since_id:
		url="http://twitter.com/statuses/user_timeline/%s.xml?since_id=%s"%(twitter_id,since_id)
	else:
		url="http://twitter.com/statuses/user_timeline/%s.xml"%(twitter_id)
	result = urlfetch.fetch(url)
	if result.status_code == 200:
		content=result.content
		m= re.findall(r"(?i)<id>([^<]+)</id>\s*<text>(?!@)([^<]+)</text>", content)
	for x in reversed(m):
		id=x[0]
		text=x[1]
		if text.find('@',1) == -1 :
			ret = xmpp.send_message(weibo_bot, unescape(text))
			msg=Twitter()
			msg.id=id
			msg.put()
		
def make_cookie_header(cookie):
	ret = ""
	for val in cookie.values():
		ret+="%s=%s; "%(val.key, val.value)
	return ret

def unescape(text):
   """Removes HTML or XML character references 
	  and entities from a text string.
   from Fredrik Lundh
   http://effbot.org/zone/re-sub.htm#unescape-html
   """
   def fixup(m):
	   text = m.group(0)
	   if text[:2] == "&#":
		   # character reference
		   try:
			   if text[:3] == "&#x":
				   return unichr(int(text[3:-1], 16))
			   else:
				   return unichr(int(text[2:-1]))
		   except ValueError:
			   pass
	   else:
		   # named entity
		   try:
			   text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
		   except KeyError:
			   pass
	   return text # leave as is
   return re.sub("&#?\w+;", fixup, text)
  
def getLatest():
	msg=db.GqlQuery("SELECT * FROM Twitter ORDER BY created DESC")
	x=msg.count()
	if x:
		return msg[0].id
	else:
		return ""

latest=getLatest()
parseTwitter(twitter_id,since_id=latest)
